from .IOjson import write_json,read_json
import pandas as pd

def set_data_json(filename='data.xlsx'):
    DATA=dict()
    dataDf=pd.read_excel(filename)
    for k,v in dataDf.to_dict().items():
        DATA[k]=v[0]

    DATA['product_mz']=list(map(lambda x:float(x.strip("'"))
                                ,DATA['product_mz'].split(";")))
    DATA['product_intensity']=list(map(lambda x:float(x.strip("'"))
                                ,DATA['product_intensity'].split(";")))
    
    write_json("data.json",DATA)


def get_non_water():
    amino_acids=read_json("amino_acid.json")
    Water_weight=amino_acids[1]["Monoisotopic Mass"]
    aas=[{"Letter":"","Mass":0}]
    for aa in amino_acids[0]["info"]:
        aas.append({
            "Letter":aa["OneLetter"],
            "Mass":aa["Monoisotopic Mass"]-Water_weight
        })
    return aas


def get_all_ppfms():
    aas = get_non_water()
    ppfms=list()
    for i in range(len(aas)):
        for j in range(i,len(aas)):
            if i==0 and j==0:
                continue
            ppfms.append({
                "Mass":aas[i]["Mass"]+aas[j]["Mass"],
                "Letter":aas[i]["Letter"]+aas[j]["Letter"]
            })
    return ppfms


def get_all_icon():
    amino_acids=read_json("amino_acid.json")
    Water_weight=amino_acids[1]["Monoisotopic Mass"]
    H_weight=amino_acids[2]["Monoisotopic Mass"]/2
    aas = get_non_water()

    pps=list()
    for i in range(len(aas)):
        for j in range(i,len(aas)):
            if i==0 and j==0:
                continue
            mass = aas[i]["Mass"]+aas[j]["Mass"]
            ppf = aas[i]["Letter"]+aas[j]["Letter"]
            for charge in (1,2):
                pps.extend([
                    {
                        "ppf": ppf,
                        "type":f"b{len(ppf)} icon",
                        "mass":mass,
                        "charge":charge,
                        "m/z":(mass+charge*H_weight)/charge
                    },
                    {
                        "ppf": ppf,
                        "type":f"y{len(ppf)} icon",
                        "mass":mass,
                        "charge":charge,
                        "m/z":(mass+Water_weight+charge*H_weight)/charge
                    }
                ])
    return pps

if __name__ == "__main__":
    print(len(get_non_water()))
    print(len(get_all_icon()))
    print(get_all_icon()[0])
    print(len(get_all_ppfms()))
    print(get_all_ppfms())